#include "StdAfx.h"
#include "Utils.h"

void UDialog(CWnd* pParent) {
	/*
	CHtmlPanel* pnl = new CHtmlPanel(pParent);
	pnl->Create(2000, pParent);
	pnl->ShowWindow(SW_SHOW);
	*/

}

void mi(long n) {
	TCHAR sz[256];
	wsprintf(sz, L"%d", n);
	MessageBox(NULL, sz, L"OK", MB_OK);
}

void swap ( long& a, long& b )
{
  int c(a); a=b; b=c;
}

void mis(long amount, ...) {
	TCHAR sz[256] = L"";
	TCHAR szTemp[256];

	long arg;
	va_list argptr;
	va_start(argptr, amount);
	
	for(int i = 0; i < amount; i++) {
		arg = va_arg(argptr, long);
		wsprintf(szTemp, L"%d ", arg);
		//MessageBox(NULL, szTemp, L"OK", MB_OK);
		wcscat_s(sz, 256, szTemp);
	}
    va_end (argptr);

	MessageBox(NULL, sz, L"OK", MB_OK);
}

void mh(DWORD n) {
	TCHAR sz[256];
	wsprintf(sz, L"%x", n);
	MessageBox(NULL, sz, L"OK", MB_OK);
}

void ms(LPCWSTR sz) {
	MessageBox(NULL, sz, L"OK", MB_OK);
}


void ma(LPCSTR sz) {
	TCHAR szw[256];
	u::CharToWide(sz, szw, 256);	
	ms(szw);
}

void mb(BYTE* b, int nBytes) {	
	CString s;

	TCHAR sz[256];
	for(int i = 0; i < nBytes; i++) {
		wsprintf(sz, L"%02x ", b[i]);
		s += sz;
	}
	
	ms(s);
}

void showLastError ()
{
	TCHAR strError[256];
	TCHAR strOut[256];

	DWORD n = GetLastError();
	FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, n, LANG_NEUTRAL, strError, 200, NULL);
	wsprintf (strOut, L"Error %d: %s", n, strError);
	MessageBox (NULL, &strOut[0], L"GetLastError", MB_OK | MB_ICONERROR);
}

u::u(void)
{
}

u::~u(void)
{
}

void u::MakeClick(POINT po) {
	HWND hWnd = WindowFromPoint(po);
	ScreenToClient(hWnd, &po);

	PostMessage(hWnd, WM_LBUTTONDOWN, 0, MAKELPARAM(po.x, po.y));
	PostMessage(hWnd, WM_LBUTTONUP, 0, MAKELPARAM(po.x, po.y));
}

void u::SendClick(CWnd* wnd, POINT po) {
	wnd->SendMessage(WM_LBUTTONDOWN, 0, MAKELPARAM(po.x, po.y));
	wnd->SendMessage(WM_LBUTTONUP, 0, MAKELPARAM(po.x, po.y));
}

void u::PostClick(CWnd* wnd, POINT po) {
	wnd->PostMessage(WM_LBUTTONDOWN, 0, MAKELPARAM(po.x, po.y));
	wnd->PostMessage(WM_LBUTTONUP, 0, MAKELPARAM(po.x, po.y));
}

// Convert a string describes decimal or hexadecimal number to byte array
// Example: 0A123E -> [0A][12][3E]
// Be careful: 0A123E2 -> lost the last [2]
// Return length of byte array
int u::StringToByteArray(char *sz, BYTE *ba) {
	int al, ah;
	int i = 0;

	_strupr_s(sz, 256);
	for(; UINT(i + 1) < strlen(sz); i += 2) {
		ah = sz[i] > '9' ? sz[i] - 55 : sz[i] - '0';
		al = sz[i + 1] > '9' ? sz[i + 1] - 55 : sz[i + 1] - '0';
		ba[i / 2] = ah * 16 + al;
	}
	return i / 2;
}

void u::ByteArrayToString(BYTE* b, char* sz, int nBytes) {
	int i;
	for(i = 0; i < nBytes; i++) {
		sprintf_s(sz + i * 2, nBytes * 2 + 1, "%02x", b[i]);
	}
}

void u::CharToWide(LPCSTR sz, LPWSTR szw, int nBytes) {	
	MultiByteToWideChar(CP_ACP, 0, sz, nBytes, szw, nBytes);	
}

BOOL u::IsFileExist(LPCWSTR szFile) {
	DWORD       fileAttr;

    fileAttr = GetFileAttributes(szFile);
    if (0xFFFFFFFF == fileAttr)
        return false;
    return true;
}

int u::ByteArrayCompare(BYTE* b1, BYTE* b2, int nBytes) {
	for(int i = 0; i < nBytes; i++) {
		if(b1[i] != b2[i]) {
			return b1[i] - b2[i];
		}
	}

	return 0;
}

void u::StringToLower(TCHAR* sz) {
	_wcslwr_s(sz, 256);
}

HWND u::GetParentWindow(HWND hWnd, int iLayer) {
	// To get top most parent, set iLayer = -1;			
	HWND hWndParent = hWnd;
	for(int i = 0; i != iLayer; i++) {
		hWnd = GetParent(hWnd);		
		if(hWnd == 0) {
			break;
		} else {
			hWndParent = hWnd;			
		}
	}
	return hWndParent;
}

void u::ShowCenter(CWnd *MyDialogPtr)
{
  CPoint   Point;
  CRect    DialogRect;
  CRect    ParentRect;
  int      nWidth;
  int      nHeight;
  CWnd     *DesktopWindow = NULL;
//  CWnd     *MainWindow;

  // Get the size of the dialog box.
  MyDialogPtr->GetWindowRect(DialogRect);

  // Get the main window.
  //MainWindow = AfxGetApp()->m_pMainWnd;

  // Determine if the main window exists. This can be useful when
  // the application creates the dialog box before it creates the
  // main window. If it does exist, retrieve its size to center
  // the dialog box with respect to the main window.
 // if (MainWindow != NULL)
 //    MainWindow->GetClientRect(ParentRect);
  // If the main window does not exist, center with respect to
  // the desktop window.
//  else
  //{
     DesktopWindow = MyDialogPtr->GetDesktopWindow();
     DesktopWindow->GetWindowRect(ParentRect);
  //}

  // Calculate the height and width for MoveWindow().
  nWidth = DialogRect.Width();
  nHeight = DialogRect.Height();

  // Find the center point and convert to screen coordinates.
  Point.x = ParentRect.Width() / 2;
  Point.y = ParentRect.Height() / 2;
  //if (MainWindow)
     //MainWindow->ClientToScreen(&Point);
  //else
     DesktopWindow->ClientToScreen(&Point);

  // Calculate the new X, Y starting point.
  Point.x -= nWidth / 2;
  Point.y -= nHeight / 2;

  // Move the window.
  MyDialogPtr->MoveWindow(Point.x, Point.y, nWidth, nHeight, FALSE);
  MyDialogPtr->ShowWindow(SW_SHOW);
}

// LPWSTR szFileName: out/in : in: default file name; out: chosen file path; szFilter: xxx
BOOL u::ShowOpenFile(HWND hWnd, LPWSTR szFileName, LPWSTR szFilter) {
	OPENFILENAME ofn;
	ofn.lStructSize       = sizeof (OPENFILENAME) ;
	ofn.hwndOwner         = hWnd;
	ofn.hInstance         = NULL ;
	ofn.lpstrFilter		  = szFilter;
	ofn.lpstrCustomFilter = NULL ;
	ofn.nMaxCustFilter    = 0 ;
	ofn.nFilterIndex      = 0 ;          
	ofn.nMaxFile          = MAX_PATH ;
	ofn.lpstrFileTitle    = NULL ;          
	ofn.nMaxFileTitle     = MAX_PATH ;
	ofn.lpstrInitialDir   = NULL ;
	ofn.lpstrTitle        = NULL ;
	ofn.Flags             = 0 ;             
	ofn.nFileOffset       = 0 ;
	ofn.nFileExtension    = 0 ;
	ofn.lpstrDefExt       = TEXT ("") ;
	ofn.lCustData         = 0L ;
	ofn.lpfnHook          = NULL ;
	ofn.lpTemplateName    = NULL ;
	ofn.lpstrFile         = szFileName;
	ofn.Flags             = OFN_HIDEREADONLY ;
     
	return GetOpenFileName (&ofn);
}

bool u::ShowGetFolder(LPWSTR folderpath, const TCHAR* szCaption = NULL, HWND hOwner = NULL)
{
   bool retVal = false;
   BROWSEINFO bi;
   memset(&bi, 0, sizeof(bi));
   bi.ulFlags   = BIF_USENEWUI | BIF_RETURNONLYFSDIRS;
   bi.hwndOwner = hOwner;
   bi.lpszTitle = szCaption;

   // must call this if using BIF_USENEWUI
   ::OleInitialize(NULL);

   LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);
   if(pIDL != NULL)
   {
      if(::SHGetPathFromIDList(pIDL, folderpath) != 0)
      {
         retVal = true;
      }
      CoTaskMemFree(pIDL);
   }

   ::OleUninitialize();
   return retVal;
}